package top.lgd.controller.person;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.enums.AccontFlowTypeEnum;
import top.lgd.enums.AccountAttrEnum;
import top.lgd.pojo.entity.person.Account;
import top.lgd.pojo.entity.person.AccountFlow;
import top.lgd.pojo.res.person.AccountFlowBriefRes;
import top.lgd.service.person.AccountFlowService;
import top.lgd.service.person.AccountService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/personAccountFlow")
public class AccountFlowAdminController extends LgdController {

    @Autowired
    private AccountFlowService accountFlowService;
    @Autowired
    private AccountService accountService;

    /**
     * 查询 个人账户流水 列表
     *
     * @param current     当前页码
     * @param pageSize    每页页数
     * @param accountFlow 账户表查询条件
     * @return
     */
    @GetMapping("queryList")
    public LgdTable<AccountFlow> queryList(@RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long pageSize, AccountFlow accountFlow) {
        QueryWrapper<AccountFlow> queryWrapper = new QueryWrapper(accountFlow);
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByAsc(LgdConstants.COLUMN_ID);
        Page<AccountFlow> pageInfo = accountFlowService.page(new Page(current, pageSize), queryWrapper);
        return getTable(pageInfo);
    }

    /**
     * 查询 个人账户流水[简介] 列表
     *
     * @param accountFlow 账户表查询条件
     * @return
     */
    @GetMapping("queryBriefList")
    public LgdTable<AccountFlowBriefRes> queryBriefList(AccountFlow accountFlow) {
        QueryWrapper<AccountFlow> queryWrapper = new QueryWrapper(accountFlow);
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByDesc(LgdConstants.COLUMN_ID);
        Page<AccountFlow> pageInfo = accountFlowService.page(new Page(accountFlow.getCurrent(), accountFlow.getPageSize()), queryWrapper);
        List<AccountFlowBriefRes> dataList = new ArrayList<>();
        pageInfo.getRecords().forEach(f -> {
            AccountFlowBriefRes accountFlowBriefRes = new AccountFlowBriefRes();
            String brief = f.getFlowDate().toString();

            if(AccountAttrEnum.A.name().equals(f.getAccountAttr())){
                if (AccontFlowTypeEnum.IN.name().equals(f.getFlowType())) {
                    brief += " (资产增加)：" + f.getBeforeAmount() + " + [" + f.getFlowAmount() + "] = " + f.getAfterAmount();
                } else {
                    brief += " (资产减少)：" + f.getBeforeAmount() + " - [" + f.getFlowAmount() + " ] = " + f.getAfterAmount();
                }
            }else{
                if (AccontFlowTypeEnum.IN.name().equals(f.getFlowType())) {
                    brief += " (负债减少)：" + f.getBeforeAmount() + " - [" + f.getFlowAmount() + "] = " + f.getAfterAmount();
                } else {
                    brief += " (负债增加)：" + f.getBeforeAmount() + " + [" + f.getFlowAmount() + " ] = " + f.getAfterAmount();
                }
            }

            accountFlowBriefRes.setBrief(brief);
            Account account = accountService.queryAccount(f.getTradeAccountId());
            if (null != account) {
                accountFlowBriefRes.setBizDesc(f.getBizDesc() + " | 对方账户：" + account.getAccountName());
            } else {
                accountFlowBriefRes.setBizDesc(f.getBizDesc() + " | 对方账户：无");
            }
            dataList.add(accountFlowBriefRes);
        });
        return getTable(pageInfo, dataList);
    }
}